package com.karl.loading.base;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by Karl on 2017/8/25 0025.
 * desc: Loading系列自定义View基类
 */

public abstract class BaseView extends View{

    protected final int DEFAULT_WIDTH = 50;

    protected final int DEFAULT_HEIGHT = 50;

    protected final int defaultPadding = 10;

    protected int DEFAULT_STROKE_WIDTH = 2;

    protected int DEFAULT_COLOR = Color.parseColor("#999999");

    protected ValueAnimator valueAnimator;


    public BaseView(Context context) {
        this(context,null);
    }

    public BaseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(
                measureXY(DEFAULT_WIDTH,widthMeasureSpec),
                measureXY(DEFAULT_HEIGHT,heightMeasureSpec)
        );
    }

    private int measureXY(int defaultSize,int measureSpec){
        int defSize = dp2px(defaultSize);
        int specSize = MeasureSpec.getSize(measureSpec);
        int specMode = MeasureSpec.getMode(measureSpec);

        int result = 0;
        switch (specMode){
            case MeasureSpec.UNSPECIFIED:
            case MeasureSpec.AT_MOST:
                result = Math.min(defSize,specSize);
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }


    /**
     * 开启动画
     * @param repeatCount repeatCount
     * @param repeatMode repeatMode
     * @param time time
     */
    protected void startAnim( int repeatCount, int repeatMode, long time){
        valueAnimator = initAnim();
        if (valueAnimator!=null){
            valueAnimator.setDuration(time);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setRepeatCount(repeatCount);
            valueAnimator.setRepeatMode(repeatMode);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    onUpdateAnim(valueAnimator);
                }
            });

            if (!valueAnimator.isRunning()){
                valueAnimator.start();
            }
        }
    }

    /**
     * 停止动画
     */
    public void stopAnim(){
        if (valueAnimator!=null){
            clearAnimation();

            valueAnimator.setRepeatCount(0);
            valueAnimator.cancel();
            valueAnimator.end();

        }
    }

    /**
     * 设置颜色
     * @param color 颜色设置
     */
    public abstract void setColor(int color);

    /**
     * 开启动画
     */

    protected abstract ValueAnimator initAnim();

    public abstract void startAnim();

    public abstract void startAnim(long time);

    /**
     * 动画更新
     * @param animator animator
     */
    protected abstract void onUpdateAnim(ValueAnimator animator);


    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public int dp2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dp
     *
     * @param pxValue px值
     * @return dp值
     */
    public int px2dp(float pxValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * sp转px
     *
     * @param spValue sp值
     * @return px值
     */
    public int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * px转sp
     *
     * @param pxValue px值
     * @return sp值
     */
    public int px2sp(float pxValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
}
